<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class PersonInfo extends CI_Controller{
    function __construct() {
        parent::__construct();
        //$this->load->library('PersonInfo');
    }
    public function index($userid){
        header("Access-Control-Allow-Origin:*");
        $this->dbConnectsql = $this->load->database("sqlserver_hr",TRUE);
        $query = $this->dbConnectsql->query("select t1.A0101 username,t1.A0190 userno,t1.A0144 entry,t1.A0152 gongling,t1.A0117 useraddress,t1.A0222 usertel,
                                            t1.EMAIL email,t2.MC0000 comp,t3.CONTENT dept,t4.MC0000 station,t5.MC0000 post,
                                            t1.fatherName fatherName,t1.fatherBirth,t1.motherName,t1.motherBirth,t1.tel tel,t1.address address from A01 as t1
                                            inner join BM_24 as t2 on t1.A0110 = t2.BM0000
                                            inner join B01 as t3 on t1.DEPT_CODE = t3.DEPT_CODE
                                            inner join E01 as t4 on t1.E0101 = t4.E0101
                                            inner join BM_HX as t5 on t1.C0107 = t5.BM0000
                                            where t1.A0190 = '$userid'");
        $resDate = $query->row_array();
        //var_dump($resDate);
        //echo substr($resDate['entry'], 0,10);
        $result = array("username"    => $resDate['username'],
                        "userno"      => $resDate['userno'],
                        "entry"       => substr($resDate['entry'], 0,10),
                        "gongling"    => $resDate['gongling'],
                        "useraddress" => $resDate['useraddress'],
                        "usertel"     => $resDate['usertel'],
                        "email"       => $resDate['email'],
                        "comp"        => $resDate['comp'],
                        "dept"        => $resDate['dept'],
                        "station"     => $resDate['station'],
                        "post"        => $resDate['post'],
                        "fatherName"  => $resDate['fatherName'],
                        "fatherBirth" => $resDate['fatherBirth'],
                        "motherName"  => $resDate['motherName'],
                        "motherBirth" => $resDate['motherBirth'],
                        "tel"         => $resDate['tel'],
                        "address"     => $resDate['address']);
        $this->load->view("PersonInfo",$result);
    }
    public function Login(){
        if (isset($_GET['code'])){
            $code = $_GET['code'];
            //$res = $this->Data_model->get_single_data("name = '中兴精密'","WEIXININFO");
            $res = $this->Data_model->get_single_data(array("name" => "中兴精密",
                                                            "type" => 'PersonInfo_50'),"WEIXININFO");
            $this->get_userId($res['access_token'], $code,$res['agentid']);
        }else{
            echo "NO CODE";
        }
    }
    
    function get_userId($access_token,$code,$agentId){
        if($access_token){
            $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=".$access_token."&code=".$code."&agentid=".$agentId;
            $json=self::http_request_json($url);//这个地方不能用file_get_contents
            $data=json_decode($json,true);
            if(isset($data['errcode'])){
                if($data['errcode'] == "40001" or $data['errcode'] == '42001' or $data['errcode'] == '40014'){
                    $this->get_access_token();
                    $newurl = "<?=base_url()?>/PersonInfo/Login?code=".$code;
                    echo "<script language='javascript' type='text/javascript'>";  
                    echo "window.location.href='".$newurl."'";  
                    echo "</script>"; 
                    
                }else{
                    echo "页面请求错误，请检查！";
                }
            }else{
                $this->index($data['UserId']);
            }
        }else{
            $this->get_access_token();
            $this->getLoginInfo();
        }
    }
    function get_access_token(){
        $result = $this->Data_model->get_single_data(array("name" => "中兴精密",
                                                            "type" => 'PersonInfo_50'),"WEIXININFO");
        $corpid = $result['corpid'];
        $corpsecret = $result['corpsecret'];
        $url="https://qyapi.weixin.qq.com/cgi-bin/gettoken?&corpid=".$corpid."&corpsecret=".$corpsecret;
        $json=self::http_request_json($url);//这个地方不能用file_get_contents
        $data=json_decode($json,true);
        if(isset($data['access_token'])){
            $this->Data_model->update_data(array("name" => "中兴精密",
                                                            "type" => 'PersonInfo_50'),array('access_token' => $data['access_token']),'WEIXININFO');
        }else{
            return "获取access_token错误";
        }        
    }
    //因为url是https 所有请求不能用file_get_contents,用curl请求json 数据
    function http_request_json($url){  
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;  
    }
    function get_checkdata($date,$userid,$tablename){
        //$resData = $this->Data_model->get_single_data("CHECKDATE = '".$date."' and EMPNO = '".$userid."'",$tablename);
        $resData = $this->Data_model->get_data(array("CHECKDATE" => $date,
                                                        "EMPNO" => $userid),"","",-1,-1,$tablename);
        return $resData;
    }
	function update(){
        header("Access-Control-Allow-Origin:*");
        $this->dbConnectsql = $this->load->database("sqlserver_hr",TRUE);
        $userno          = trim($_GET['userno']);
        $usertel         = trim($_GET['usertel']);
        $email           = trim($_GET['email']);
        $useraddress     = trim($_GET['useraddress']);
        $fatherName      = trim($_GET["fatherName"]);
        $fatherBirth     = trim($_GET['fatherBirth']);
        $motherName      = trim($_GET['motherName']);
        $motherBirth     = trim($_GET['motherBirth']);
        $tel             = trim($_GET['tel']);
        $address         = trim($_GET['address']);
        $query = $this->dbConnectsql->query("update A01 set A0222 = '$usertel',EMAIL = '$email',A0117 = '$useraddress',fatherName = '$fatherName',"
                . "fatherBirth = '$fatherBirth',motherName = '$motherName',motherBirth = '$motherBirth',tel = '$tel',address = '$address' where A0190 = '$userno'");
		//echo "update A01 set A0222 = '$usertel',EMAIL = '$email',A0117 = '$useraddress',fatherName = '$fatherName',"
        //        . "fatherBirth = '$fatherBirth',motherName = '$motherName',motherBirth = '$motherBirth',tel = '$tel',address = '$address' where A0190 = '$userno'";
	}
	function updateSuccess(){
		$userid = trim($_GET['userid']);
		$this->index($userid);
	}
}
